const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();

const app = getApp()
Page({
  data: {
    textLength: 0,
    type: 0,
    key: '',
    texts: [],
    inputValue: ''
  },
  onShow() {},
  onLoad(options) {
    this._initData();
    const { type = 1, key, value = '' } = options;
    this.setData({type, key});
    if (value) {
      this.setData({
        inputValue: decodeURIComponent(value)
      });
    }
  },
  _initData() {
    this.setData({
      texts: {
        '0': { title: '我的昵称', prompt: '起个有吸引力的昵称很重要', placeholder: '输入昵称'},
        '1': { title: '我的学校', prompt: '填写真实学校会提高你的匹配率', placeholder: '输入我的学校名称' },
        '2': { title: '我的公司', prompt: '填写公司，认识更多有趣的人', placeholder: '请输入我的公司' },
        '3': { title: '我的职业', prompt: '填写职业，认识更多有趣的人', placeholder: '请输入我的职业' },
      }
    });
  },
  inputChange(target) {
    const { value } = target.detail;
    this.setData({
      inputValue: value,
      textLength: value.length
    });
  },
  
  sure() {
    if (!this.data.inputValue) {
      wx.showToast({ title: '信息不能为空', icon: 'none', duration: 2000 });
      return;
    }
    // const actions = {
    //   'school': this._edit,
    //   'company': this._edit,
    //   'profession': this._edit,
    // };
    // actions[this.data.key]();
    this._edit();
  },
  clear() {
    this.setData({
      inputValue: '',
      textLength: 0
    });
  },

  async _edit() {
    let params = {};
    params[this.data.key] = this.data.inputValue;
    console.log('🚀 ~ file: index.js ~ line 51 ~ _edit ~ params', params);
    const {returnCode = '', bean = {}} = await request({
      url: api.editUser,
      method: 'put',
      data: params
    });
    if (returnCode === '0000') {
      wx.showToast({ title: '资料修改成功', icon: 'none', duration: 2000 });
      wx.navigateBack({
        delta: 1
      });
    }
  }
});
